package com.example.transaction_springboot.service;

import com.example.transaction_springboot.common.Page;
import com.example.transaction_springboot.controller.request.BookRequest;
import com.example.transaction_springboot.entity.Book;
import com.example.transaction_springboot.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BookService {


    @Autowired
    private BookMapper bookMapper;

    public List<Book> list() {
        return bookMapper.listBook();
    }

    public Page<Book> page(BookRequest bookRequest) {
        Page<Book> page = new Page<>();
        bookRequest.setPageNum((bookRequest.getPageNum() - 1) * bookRequest.getPageSize());
        List<Book> bookList = bookMapper.selectByPage(bookRequest);
        Integer total = bookMapper.selectCountByPage(bookRequest);
        page.setList(bookList);
        page.setTotal(total);
        return page;
    }


    public void addBook(Book book) {
        book.setCreatetime(new Date());
        book.setUpdatetime(new Date());
        bookMapper.addBook(book);
    }

    public Book selectById(Integer id) {
        return bookMapper.selectById(id);
    }

    public void updateById(Book book) {
        book.setUpdatetime(new Date());
        bookMapper.updateById(book);
    }

    public void deleteById(Integer id) {
        bookMapper.deleteById(id);
    }
}
